--
-- @Author:      feilong
-- @DateTime:    2019-10-19 14:02:03
-- @Description: 库存的管理
--[[
    --基础库存
    -- ["fee_rate"] = 1000, --抽水率（百分比
    -- ["base_min"] = 1000000, --库存下限值
    -- ["base_max"] = 10000000, --库存上限值
    -- ["base_normal"] = 5000000, --正常值
    -- ["win_control_rate"] = 5000, --吃分控制率（万分比
    -- ["lose_control_rate"] = 5000, --放分控制率（万分比
    -- ["warn_control_rate"] = 8000, --警戒控制率（万分比
    -- ["base_lose_limit"] = 950000, --库存单局输钱上限
    --奖励库存
    -- ["award_rate"] = 500, --抽水率（百分比
    -- ["award_min"] = 1000000, --库存下限值
    -- ["award_max"] = 10000000, --库存上限值
    -- ["tigger_rate_min"] = 1500, --触发概率下限(万分比)
    -- ["tigger_rate_max"] = 2500, --触发概率上限(万分比)
    -- ["award_lose_limit"] = 5000, --单局输钱上限(元)
    -- ["special_rate_bank_pair"] = 10, --庄对概率(百分比)
    -- ["special_rate_play_pair"] = 10, --闲对概率(百分比)
    -- ["special_rate_tie"] = 10, --和牌概率(百分比)    
]]


local cocall = require "cocall"
local constant = require "constant"
local util = require "util"

local table_insert = table.insert
local math_random = math.random
local table_copy = table.copy
local table_remove = table.remove
local string_split = string.split
local math_abs = math.abs
local math_floor = math.floor

local CONTROL_RESULT_WIN = 1
local CONTROL_RESULT_LOSE = 2

local StoreManager = class("StoreManager")

function StoreManager:ctor(message_dispatch)

end

function StoreManager:init()
	self.user_manager = global.user_manager
	self.room_config = global.room_config
    self.table_id = self.room_config:getTableId()
    self.table_type = self.room_config:getTableType()
    self.table_gid = self.room_config:getTableGid()
    self.game_type = self.room_config:getGameType()
end

--库存控制的结果
--返回游戏控制类型结果，要控制的玩家uid
--百人场不进行个人控制
--控制类型----------
-- 0 => '随机',
-- 1 => '库存1赢钱',
-- 2 => '库存1输钱',
-- 3 => '奖励库存输钱',
-- 4 => '个人库存赢钱',
-- 5 => '个人库存输钱',
-- 6 => '触发奖池',
-- 7 => '触发免费次数', 
function StoreManager:getSystemStoreResult(banker)
    local player_list = self.user_manager:getRealBetUsers() --下过注的真玩家列表
    local robot_store = self.room_config:getRobotStore()  
    local gm_warn = self.room_config:getWarnCoins()
    local is_have_robot = self.user_manager:haveRobot()
    local is_hundred_game = self.room_config:isHundredGame()
    local base_warn = robot_store.base_min or 0 --库存下限值
    local uid_list = {}
    for k, v in pairs(player_list) do 
        table_insert(uid_list, v:getUid())
    end
    if banker and banker:isPlayer() then 
        table_insert(uid_list, banker:getUid())
    end
    --没有真人在玩
    if not next(uid_list) then 
        return constant.SYSTEM_NO_CONTROL --系统库存策略 随机
    end
    self:print("玩家列表",  uid_list)
    --总库存    
  	local system_coins = self:getTotalStore()   
    self:print("库存控制system_coins",  system_coins, base_warn)
    if system_coins > base_warn then

        local uid,curr_control_coins,control_rate,weight = self:getMaxPlayerGMControl(uid_list, is_hundred_game)
        --必须满足GM库存水位线
        local match_gm_store = self:isMatchGMStore(curr_control_coins, gm_warn)
        dbglog("get_personal_system_store_result11",uid, curr_control_coins, control_rate, weight)
        if uid and curr_control_coins and control_rate and math_random(1,100) <= control_rate and match_gm_store then
            if curr_control_coins > 0 then
                return constant.PERSONAL_STORE_WIN,uid,control_rate
            elseif curr_control_coins < 0 then
                return constant.PERSONAL_STORE_LOSE,uid,control_rate
            end
        end

        --再检查是否可以触发个人控制(非百人类以及捕鱼都不触发)
        if not is_hundred_game and game_def.get_game_type(table_type) ~= table_def.GAME_TYPE_FISHING and #player_list == 1 then --只有一个真实玩家才会触发个控
            local uid,curr_control_coins,control_rate,weight = self:getMaxPlayerPersonalControl(uid_list)
            local match_gm_store = self:isMatchGMStore(curr_control_coins, gm_warn)
            dbglog("get_personal_system_store_result22",uid,curr_control_coins,control_rate,weight)
            if uid and curr_control_coins and control_rate and math_random(1,100) <= control_rate and match_gm_store then
                if curr_control_coins > 0 then
                    return constant.PERSONAL_STORE_WIN,uid,control_rate
                elseif curr_control_coins < 0 then
                    return constant.PERSONAL_STORE_LOSE,uid,control_rate
                end
            end
        end        
    end

    --有机器人才触发系统库存控制
    local result = 0
    if is_have_robot then
        -- local ok, system_result = R().robotmgr(1):call('.msg_handler', 'check_system_store', robot_type)  
        return self:checkSystemStore()    
    end

    return result
end

-- --个人控制(水果机捕鱼在用)
-- function StoreManager:getPlayerPersonalControlResult(uid, gm_warn)
--     if not uid then
--         return 0
--     end

--     local ok,ret = R().basesvr({key=uid}):call('.msg_handler', 'is_affected',uid)
--     if not ok or not ret then
--         errlog(uid, "get_player_store error because ok is nil")
--         return 0
--     end

--     local match_gm_store = self:isMatchGMStore(ret.control_coins,gm_warn)
--     if ret.control_coins and ret.control_rate and math_random(1,100) <= ret.control_rate and match_gm_store then
--         if ret.control_rate > 0 then
--             return constant.PERSONAL_STORE_WIN
--         elseif ret.control_rate < 0 then
--             return constant.PERSONAL_STORE_LOSE
--         end
--     end
    
--     return 0
-- end





-------------------红包扫雷专用的-------------------start

--库存控制的结果
--返回游戏控制类型结果，要控制的玩家uid
function StoreManager:getSystemStoreResultHBSL(uid_list, sender)
    local robot_store = self.room_config:getRobotStore()  
    local gm_warn = self.room_config:getWarnCoins()
    local base_warn = robot_store.base_min or 0 --库存下限值
    local roomData = self.room_config:getRoomData()
    --总库存    
    local system_coins = self:getTotalStore()    
    self:print("__________1___getSystemStoreResultHBSL__",uid_list,  system_coins, base_warn)
    local is_hundred_game = roomData.is_hundred_game
    if system_coins > base_warn then
        local uid, curr_control_coins, control_rate, weight = self:getMaxPlayerGMControl(uid_list, is_hundred_game)
        local match_gm_store = self:isMatchGMStore(curr_control_coins, gm_warn)
        self:print("__________2___getSystemStoreResultHBSL__ uid:", uid, " curr_control_coins:",curr_control_coins," control_rate:",control_rate," match_gm_store:",match_gm_store)
        if  uid and curr_control_coins and control_rate and math_random(1,100) <= control_rate and match_gm_store then
            if uid and curr_control_coins > 0 then
                return constant.PERSONAL_STORE_WIN,uid
            elseif uid and curr_control_coins < 0 then
                return constant.PERSONAL_STORE_LOSE,uid
            end
        end
    end
    if sender then 
        return self:checkSystemStore()    
    end
    return constant.SYSTEM_NO_CONTROL   
end

-- --库存控制的结果
-- --返回游戏控制类型结果，要控制的玩家uid
-- function StoreManager:getStoreResult(uid_list)
--     local robot_store = self.room_config:getRobotStore()  
--     local gm_warn = self.room_config:getWarnCoins()
--     local base_warn = robot_store.base_min or 0 --库存下限值
--     local roomData = self.room_config:getRoomData()
--     --总库存    
--     local system_coins = self:getTotalStore()    
--     self:print(uid,"getStoreResult",system_coins,base_warn)
--     local is_hundred_game = roomData.is_hundred_game
--     if system_coins > base_warn then
--         local uid, curr_control_coins, control_rate, weight = self:getMaxPlayerGMControl(uid_list, is_hundred_game)
--         local match_gm_store = self:isMatchGMStore(curr_control_coins, gm_warn)
--         self:print("get_personal_system_store_result11 uid:",uid," curr_control_coins:",curr_control_coins," control_rate:",control_rate," match_gm_store:",match_gm_store)
--         if math_random(1,100) <= control_rate then
--             if uid and curr_control_coins > 0 then
--                 if match_gm_store then
--                     return constant.PERSONAL_STORE_WIN,uid
--                 end
--             elseif uid and curr_control_coins < 0 then
--                 if match_gm_store then
--                     return constant.PERSONAL_STORE_LOSE,uid
--                 end
--             end
--         end
--     end
--     return self:checkSystemStore()    
-- end

-------------------红包扫雷专用的-------------------end






--指定机器类型的库存值
function StoreManager:getTotalStore()
    -- local robot_type = self.room_config:getRobotType()
    local ok, coins = R().exdbsvr(1):call('.system_coins_mgr', 'get_system_coins', 0, self.table_type)
    if not ok then 
    	coins = 0
    end
    return coins
end

--取个人控制状态
function StoreManager:getPlayerGMControl(uid)
    local ok,ret = R().basesvr({key=uid}):call('.msg_handler','get_player_gm_control',uid)
    if not ok then
        errlog(uid, "get_player_gm_control error because ok is nil")
        return
    end
    self:print("_______getPlayerGMControl__",ret)
    return {
        weight = ret.control_weight or 0,
        win_rate = ret.win_rate or 0,
        lose_rate = ret.lose_rate or 0,
        control_coins = ret.curr_control_coins or 0,
        is_control_hundred_game = ret.is_control_hundred_game,
    }
end
-- --取个人控制状态
-- function StoreManager:getPlayerStore(uid)
--     local ok,ret = R().basesvr({key=uid}):call('.msg_handler','get_curr_control_data',uid)
--     if not ok then
--         errlog(uid, "get_player_store error because ok is nil")
--         return
--     end
--     return {
--         curr_control_status = ret.curr_control_status, --控件状态
--         curr_control_coins = ret.curr_control_coins, --控制金币
--         curr_control_weight = ret.curr_control_weight, --控件权重
--         is_hundred_game = ret.is_hundred_game, -- 1 百人场生效 0 百人场不生效个人控制
--     }
-- end

--获取权限值最大的玩家GM控制
function StoreManager:getMaxPlayerGMControl(uid_list,is_hundred_game)
    local tasks = {}
    local callback = function(...)
    	return self:getPlayerGMControl(...)
    end
    for _, uid in pairs(uid_list) do
        table_insert(tasks, { f = callback, id = uid, params = {uid} })
    end    
    local ok,results = cocall(5,tasks) --多协程调用
    if not ok then
        errlog('failed to cocall', results)
        return
    end

    self:print("GM控制个人权重列表rrrrrrrrrr", results)
 
    local power_uid_map_list = {}
    local max_weight
    for uid,ret in pairs(results) do
        if ret.control_coins ~= 0 and (is_hundred_game == false or ret.is_control_hundred_game == 1) then
            power_uid_map_list[ret.weight] = power_uid_map_list[ret.weight] or {}
            local player_data = {
                uid = uid,control_coins = ret.control_coins,
                win_rate = ret.win_rate,lose_rate = ret.lose_rate
            }
            table_insert(power_uid_map_list[ret.weight],player_data)

            if not max_weight or ret.weight > max_weight then
                max_weight = ret.weight
            end
        end
        
    end

    print("rrrrrrrrrr",max_weight, power_uid_map_list)
    if max_weight and #power_uid_map_list[max_weight] > 0 then
        local index = math_random(1,#power_uid_map_list[max_weight])
        local uid = power_uid_map_list[max_weight][index].uid
        local control_coins = power_uid_map_list[max_weight][index].control_coins
        local win_rate = power_uid_map_list[max_weight][index].win_rate
        local lose_rate = power_uid_map_list[max_weight][index].lose_rate
        local control_rate = (control_coins >= 0) and win_rate or lose_rate        
        return uid,control_coins,control_rate,max_weight
    end
end

function StoreManager:getPlayerPersonalControl(uid)
    local ok,ret = R().basesvr({key=uid}):call('.msg_handler','get_player_personal_control',uid)
    if not ok then
        errlog(uid, "get_player_personal_control error because ok is nil")
        return
    end

    return {
        weight = ret.weight or 0,
        win_rate = ret.win_rate or 0,
        lose_rate = ret.lose_rate or 0,
        control_coins = ret.control_coins or 0,
    }
end

--获取权限值最大的玩家个人控制
function StoreManager:getMaxPlayerPersonalControl(player_list)
    local personal_tasks = {}
    local callback = function(...)
        return self:getPlayerPersonalControl(...)
    end    
    for _,uid in pairs(player_list) do
        table_insert(personal_tasks,{ f = callback, id = uid, params = {uid}})
    end
    
    local ok,results = cocall(5,personal_tasks)
    if not ok then
        errlog('failed to cocall',tostring_r(results))
        return
    end
    print_r(results)

    local power_uid_map_list = {}
    local max_weight
    for uid,ret in pairs(results) do
        if ret.control_coins ~= 0 then
            power_uid_map_list[ret.weight] = power_uid_map_list[ret.weight] or {}
            local player_data = {
                uid = uid,control_coins = ret.control_coins,
                win_rate = ret.win_rate,lose_rate = ret.lose_rate
            }
            table_insert(power_uid_map_list[ret.weight],player_data)

            if not max_weight or ret.weight > max_weight then
                max_weight = ret.weight
            end
        end
        
    end

    print("rrrrrrrrrr",max_weight,tostring_r(power_uid_map_list))
    if max_weight and #power_uid_map_list[max_weight] > 0 then
        local index = math_random(1,#power_uid_map_list[max_weight])
        local uid = power_uid_map_list[max_weight][index].uid
        local control_coins = power_uid_map_list[max_weight][index].control_coins
        local win_rate = power_uid_map_list[max_weight][index].win_rate
        local lose_rate = power_uid_map_list[max_weight][index].lose_rate
        local control_rate = (control_coins >= 0) and win_rate or lose_rate
        
        return uid,control_coins,control_rate,max_weight
    end
end

-- --获取权限值最大的玩家和他的个人库存
-- function StoreManager:getMaxUidStore(uid_list)
--     local tasks = {}
--     local callback = function(...)
--         return self:getPlayerGMControl(...)
--     end
--     for _,uid in pairs(uid_list) do
--         table_insert(tasks, { f = callback, id = uid, params = {uid} })
--     end    
--     local ok,results = cocall(5,tasks) --多协程调用
--     if not ok then
--         errlog('failed to cocall', results)
--         return
--     end
--     -- print_r(results)
--     -- dbglog("个人权限列表rrrrrrrrrr",  results)
--     local power_uid_map_list = {}
--     local max_power
--     for uid,ret in pairs(results) do
--         if ret and ret.curr_control_status > 0 and ret.curr_control_coins ~= 0 and ret.curr_control_weight > 0 and (is_hundred_game == false or ret.is_hundred_game == 1) then
--             local tb = power_uid_map_list[ret.curr_control_weight]
--             if not tb then
--                 tb = {}
--                 power_uid_map_list[ret.curr_control_weight] = tb
--             end
--             table_insert(tb, {uid = uid,curr_control_coins = ret.curr_control_coins})
            
--             if not max_power or ret.curr_control_weight > max_power then
--                 max_power = ret.curr_control_weight
--             end
--         end        
--     end
--     -- print("玩家权限列表rrrrrrrrrr",max_power, tostring_r(power_uid_map_list))
--     if max_power and #power_uid_map_list[max_power] > 0 then
--         --有多个权限一样的玩家随机选一个
--         local index = math_random(1, #power_uid_map_list[max_power]) 
--         return power_uid_map_list[max_power][index].uid, power_uid_map_list[max_power][index].curr_control_coins
--     end
-- end

--玩家控制比例
function StoreManager:getPlayerControlRate(uid, curr_control_coins)
    if not uid or not curr_control_coins then
        return 0
    end
    local control_rate = 0 --控制比
    local personal_control = self.room_config:getPersonalControl()
    local range_control_rate = personal_control.range_control_rate or {}
    for _,v in pairs(range_control_rate) do
        if curr_control_coins >= (v.min or 0) and curr_control_coins <= (v.max or 0) then
            control_rate = v.rate or 0
        end
    end
    -- print("get_player_control_rate=========",uid,control_rate)
    local ok,ret = R().basesvr({key=uid}):call('.msg_handler','get_player_control_rate',uid)
    if ok and ret then
        if curr_control_coins > 0 and ret.win_rate > 0 then
            return ret.win_rate or 0
        end
        if curr_control_coins < 0 and ret.lose_rate > 0 then
            return ret.lose_rate or 0
        end
    end
    return control_rate
end

--是否符合GM库存
--add_coins 期望加的金币
--库存警告值
function StoreManager:isMatchGMStore(add_coins, warn_coins)
    self:print("is_match_gm_store=1111",add_coins,warn_coins)
    if not add_coins or not warn_coins then
        return false
    end
    if add_coins < 0 then --输钱时,一定符合
        return true
    end
    local ok,gm_coins = R().exdbsvr(1):call('.gm_coins_mgr','get_gm_coins')
    self:print("is_match_gm_store=222",add_coins,warn_coins,gm_coins)
    --控制赢钱时gm库存要大于警报值才符合
    if add_coins > 0 and (gm_coins or 0) > warn_coins then 
        return true
    end
    return false
end


--更新系统库存
--系统总输赢 system_money_cost
--system_send_award 开奖励库存
function StoreManager:updateSystemStore(system_money_cost, system_send_award)
    self:print("_____写入系统库存____",system_money_cost, system_send_award, self.table_type, game_type)
    if system_money_cost == 0 then 
        return 
    end
	local game_type = self.room_config:getGameType()
    if system_money_cost >= 0 then --玩家输 加系统库存
        R().exdbsvr(1):send('.system_coins_mgr','add_system_coins',game_type,self.table_type,system_money_cost)
    else
        if system_send_award then--从奖励库存里面扣钱
            R().exdbsvr(1):send('.system_coins_mgr','reduce_award_coins',game_type,self.table_type,math_abs(system_money_cost))
        else--从系统库存里面扣钱
            R().exdbsvr(1):send('.system_coins_mgr','sub_system_coins',game_type,self.table_type,math_abs(system_money_cost))
        end
    end
end

--更新个人库存
--control_coins 玩家输赢币
--system_money_cost 系统总输赢
--control_result 系统控制结果
function StoreManager:updatePersionStore(system_money_cost, control_coins, control_uid, control_result )
    local gm_store_coins_add = 0
    if not control_uid then
        errlog("updatePersionStore control_uid nil !",control_coins, control_uid,control_result)
    	return 
    end    
    self:print("更新GM库存__1__", system_money_cost, control_coins, control_uid, control_result )
    if control_coins > 0 and control_result == constant.PERSONAL_STORE_WIN then
        self:print("更新GM库存__2__", system_money_cost, control_coins, control_uid, control_result )
        local ok,coins = R().basesvr({key=control_uid}):send('.msg_handler','reduce_curr_control_data', control_uid, control_coins, self.table_type, true)
        local will_cost_coins = (ok and coins) and coins or 0
        local gm_add_coins = math_abs(system_money_cost) < math_abs(will_cost_coins) and math_abs(system_money_cost) or math_abs(will_cost_coins)
        gm_store_coins_add = -gm_add_coins

    elseif control_coins < 0 and control_result == constant.PERSONAL_STORE_LOSE then
        self:print("更新GM库存__3__", system_money_cost, control_coins, control_uid, control_result )
        local ok,coins = R().basesvr({key=control_uid}):send('.msg_handler','add_curr_control_data',control_uid,math_abs(control_coins), self.table_type, true)
        local will_cost_coins = (ok and coins) and coins or 0
        local gm_add_coins = math_abs(system_money_cost) < math_abs(will_cost_coins) and math_abs(system_money_cost) or math_abs(will_cost_coins)
        gm_store_coins_add = gm_add_coins
    end

    return gm_store_coins_add
end


--废弃
-- function StoreManager:updatePersionStoreHBSL(control_coins, control_uid, control_result )
--     local gm_store_coins_add = 0
--     if not control_uid then
--         errlog("updatePersionStore control_uid nil !",control_coins, control_uid,control_result)
--         return 
--     end      
--     self:print("更新个人库存__1__", control_coins, control_uid, control_result )
--     if control_coins > 0 and control_result == constant.PERSONAL_STORE_WIN then
--        self:print("更新个人库存__2__", control_coins, control_uid, control_result )
--         R().basesvr({key=control_uid}):send('.msg_handler','reduce_curr_control_data', control_uid, control_coins, self.table_type)
--             local gm_add_coins = math_abs(control_coins)
--             --R().exdbsvr(1):send('.gm_coins_mgr','reduce_gm_coins',gm_add_coins,{uid=control_uid,table_type=self.table_type}) --减GM库存
--             gm_store_coins_add = -gm_add_coins 
--     elseif control_coins < 0 and control_result == constant.PERSONAL_STORE_LOSE then
--        self:print("更新个人库存__3__", control_coins, control_uid, control_result )
--         R().basesvr({key=control_uid}):send('.msg_handler','add_curr_control_data',control_uid,math_abs(control_coins), self.table_type)
--         local gm_add_coins = math_abs(control_coins)
--         --R().exdbsvr(1):send('.gm_coins_mgr','add_gm_coins',gm_add_coins,false,{uid=control_uid,table_type=self.table_type}) --加GM库存
--         gm_store_coins_add = gm_add_coins
--     end

--     return gm_store_coins_add
-- end

--更新抽水库存,奖励库
--total_coins 总输赢
function StoreManager:updateRobotStore(total_coins)
    if total_coins <= 0 then 
        return 
    end
    local room_data = self.room_config:getRoomData()
    if not room_data or not room_data.open_robot then
        return
    end
    local robot_store = self.room_config:getRobotStore()
    if not robot_store then
        return
    end

    local fee_rate    = (robot_store.fee_rate or 5) / 10000
    local award_rate  = (robot_store.award_rate or 5) / 10000
    local fee_coins   = fee_rate * total_coins --抽水库
    local award_coins = award_rate * total_coins --奖励库
    local game_type = self.room_config:getGameType()    
    local table_type = self.room_config:getTableType()
    self:print("_____updateRobotStore_抽水与奖励库存__", total_coins, fee_rate, award_rate)
    R().exdbsvr(1):send('.system_coins_mgr', 'update_store', game_type, table_type, fee_coins, award_coins)
    
end

--库存1 超过上限就把多的金币转到奖励库存
--库存1 低于下限时就从奖励库存转金币进来
function StoreManager:checkStoreTransfer()
    local room_data = self.room_config:getRoomData()
    if not room_data or not room_data.open_robot then
        return
    end
    local cnf = self.room_config:getRobotStore()
    if not cnf then
         return
    end
    local base_min  = cnf.base_min or 0
    local base_max  = cnf.base_max or 0
    local base_normal = cnf.base_normal or 0
    local data = {base_min = base_min,base_max = base_max,base_normal = base_normal,table_type = self.table_type}
    R().exdbsvr(1):send('.system_coins_mgr','check_store_transfer', self.game_type, data)
 end

--财神时间检查
function StoreManager:checkLastTriggerTime(uid, interval)
    -- print("check_last_trigger_time",uid,curr_time,interval)
    local curr_time = util.get_now_time()
    local ok,ret = R().basesvr({key=uid}):call('.msg_handler','check_caishen_trigger_time', uid, curr_time, interval)
    if not ok then
        return false
    end
    return ret
end

--财神检查
function StoreManager:checkTriggerCaiShenJiaDao(user, coins)
    local caishenjiadao = self.room_config:getCaiShenJiaDao()
    local uid = user:getUid()
    if not caishenjiadao.switch then
        return false
    end
    if coins < caishenjiadao.tigger_coins then
        return false
    end
    local table_type = self.table_type
    local vip_level = user:getVipLevel()
    local vip_data = self.room_config:getVipData(vip_level)
    if (caishenjiadao.room_type_rate[table_type] or 0) <= 0 then --玩法场次的基础触发概率为0,就不触发
        return false
    end
    --触发财神概率（百分比） = 当前房间概率  +  VIP加成概率 
    local rate = (caishenjiadao.room_type_rate[table_type] or 0) + (vip_data.caishen_base_rate or 0)
    if math_random(1,100) > rate then
        return false
    end
    if self:checkLastTriggerTime(uid, caishenjiadao.interval) then
        return true
    end

    return false
end    

--财神库存
function StoreManager:checkCaishenStore(uid, coins)
    local caishenjiadao_store = self.room_config:getCaiShenStore()
    local base_warn = caishenjiadao_store.base_warn
    local base_goal = caishenjiadao_store.base_goal
    local base_tigger_rate = caishenjiadao_store.base_tigger_rate
    local fee_rate = caishenjiadao_store.fee_rate

    local ok,ret = R().exdbsvr(1):call('.caishen_mgr','check_caishen_store',
    self.table_type,uid,coins,base_warn,base_goal,base_tigger_rate,fee_rate)
    -- errlog("caishen_mgr",ok,ret)
    if not ok then
        errlog("get caishen store failed")
        return false
    end
    return ret
end

--取各个库存值
function StoreManager:getStoreCoins()
    --捕鱼库存不一样
    local table_type = self.room_config:getTableType()
    if math_floor(table_type / 100) == 6  then
        local ok,data = R().exdbsvr(1):call('.fishing_mgr','get_fishing_store', table_type)
        if not ok then
            return 0,0,0 
        end
        return data.pool_control.curr,data.pool_adjust.curr,data.pool_sysback.curr
    end
    local ok, system_store = R().exdbsvr(1):call('.system_coins_mgr','get_system_store', 0, table_type)
    if not ok then
        return 0,0,0 
    end
    --基础库存，奖励库存，抽水库存
    return system_store.system_coins, system_store.award_coins, system_store.fee_coins
end

--读取系统库存策略
function StoreManager:checkSystemStore()

    local robot_store = self.room_config:getRobotStore()
    if not robot_store then
        return constant.SYSTEM_NO_CONTROL
    end
    --基础库存，奖励库存，抽水库存
    local base_coins, award_coins, fee_coins = self:getStoreCoins()

    --检查库存2
    local award_min = robot_store.award_min or 0
    local award_max = robot_store.award_max or 0
    local tigger_rate_min = robot_store.tigger_rate_min or 0
    local tigger_rate_max = robot_store.tigger_rate_max or 0
    local award_tigger_rate = 0
    self:print("check_system_store111",award_coins,award_min,award_max,tigger_rate_min,tigger_rate_max)
    if award_coins < award_min then
        award_tigger_rate = 0
    elseif award_coins >= award_min and award_coins < award_max then
        --当奖励库存处于下限值和上限值之间时  最终触发概率=触发率下限+((实时库存-下限值) /(上限值-下限值)) *(触发率上限-触发率下限)
        award_tigger_rate = (tigger_rate_min / 10000) + ((award_coins - award_min)/(award_max-award_min)) * (tigger_rate_max/10000 - tigger_rate_min/10000)
    elseif award_coins >= award_max then
        award_tigger_rate = tigger_rate_max / 10000
    end
    local rand = math_random(1,10000)
    self:print("check_system_store_award_rate", award_tigger_rate)
    if (rand / 10000) <= award_tigger_rate then --触发奖励库存
        self:print("check_system_store==award_lose 触发奖励库存 ",rand, award_tigger_rate)
        return  constant.AWARD_STORE_LOSE
    end

    --检查库存1
    local base_min = robot_store.base_min or 0 --库存1下限
    local base_max = robot_store.base_max or 0 --库存1上限
    local win_control_rate = robot_store.win_control_rate or 0 --吃分控制率(万分比)
    local lose_control_rate = robot_store.lose_control_rate or 0 --放分控制率(万分比)
    local warn_control_rate = robot_store.warn_control_rate or 0 --警戒控制率(万分比)
    local average_value = math_floor((base_min + base_max)/2)

    self:print("check_system_store222",base_coins,base_min,base_max,win_control_rate,lose_control_rate,warn_control_rate,average_value)
    local rand = math_random(1,10000) / 10000
    --实时库存低于下限值时,使用固定的警戒控制率
    if base_coins < base_min then
        local base_tigger_rate = warn_control_rate / 10000
        self:print("check_system_store==base_win11", rand, base_tigger_rate)
        if rand <= base_tigger_rate then
            return constant.SYSTEM_STORE_WIN
        end
    end
    --在下限值和平均值之间触发吃分控制率,实际吃分控制率=((平均值-实时库存) /(平均值-下限值)) * 吃分控制率
    if base_coins >= base_min and base_coins <= average_value then
        local mm = average_value - base_min
        if mm == 0 then 
            mm = 1
        end
        local base_tigger_rate = ((average_value - base_coins) / mm ) * (win_control_rate/10000)
        self:print("check_system_store==base_win22", rand, base_tigger_rate)
        if rand <= base_tigger_rate then
            return constant.SYSTEM_STORE_WIN
        end
    end
    --在平均值和上限值值之间触发放分控制率,实际放分控制率=((实时库存-平均值) /(上限值-平均值)) * 放分控制率
    if base_coins > average_value and base_coins <= base_max then
        local mm = base_max - average_value
        if mm == 0 then 
            mm = 1
        end        
        local base_tigger_rate = ((base_coins - average_value) / mm) * (lose_control_rate/10000)
        self:print("check_system_store==base_lose", rand, base_tigger_rate)
        if rand <= base_tigger_rate then
            return constant.SYSTEM_STORE_LOSE
        end
    end
    self:print("check_system_store==控制结果随机")
    return constant.SYSTEM_NO_CONTROL
end

--方便各个游戏找问题
function StoreManager:print(...)
    if self.room_config:getTableType() == 200600 then 
        dbglog("StoreManager:print ", ...)
        return 
    end
    -- dbglog("StoreManager: ", ...)
end

return StoreManager